/*
 *  This file is part of ZiggyStardust.
 *
 *  ZiggyStardust is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  ZiggyStardust is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.wisc.csvParser.plugins;

import com.wisc.csvParser.ValueObject;

/**
 *
 * @author  user
 */
public class DialogCSIArrayMetadata extends java.awt.Dialog {
    DataParserCSIArrayID.ArrayIDObject arrayObj;
    
    
    /** Creates new form DialogCSIArrayMetadata
     * @param parent
     * @param modal 
     * @param arrayObj
     */
    public DialogCSIArrayMetadata(java.awt.Frame parent, boolean modal,
            DataParserCSIArrayID.ArrayIDObject arrayObj) {
        super(parent, modal);
        initComponents();
        this.arrayObj = arrayObj;
        arrayIDLabel.setText("Array ID:" + arrayObj.id);
        update();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        columnsList = new javax.swing.JList();
        arrayIDLabel = new javax.swing.JLabel();
        upButton = new javax.swing.JButton();
        downButton = new javax.swing.JButton();
        AddButton = new javax.swing.JButton();
        DeleteButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        columnsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                columnsListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(columnsList);

        arrayIDLabel.setText("Array ID:");

        upButton.setText("UP");
        upButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                upButtonActionPerformed(evt);
            }
        });

        downButton.setText("DOWN");
        downButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downButtonActionPerformed(evt);
            }
        });

        AddButton.setText("Add");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        DeleteButton.setText("Delete");
        DeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteButtonActionPerformed(evt);
            }
        });

        editButton.setText("Edit");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        jButton1.setText("DateTime Parsing Settings");
        jButton1.setToolTipText("Configure the type and columns for datetime parsing.");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(upButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(downButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 89, Short.MAX_VALUE)
                                .addComponent(editButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(AddButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(DeleteButton))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(arrayIDLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 186, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(arrayIDLabel)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(upButton)
                    .addComponent(downButton)
                    .addComponent(AddButton)
                    .addComponent(DeleteButton)
                    .addComponent(editButton))
                .addContainerGap())
        );

        add(jPanel1, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        
        if(columnsList.getSelectedValue() == null){
           arrayObj.columns.add(new DataColumn());
        }else{
            //ToDo: add later code to clone selected column
            DataColumn newCol = ((DataColumn)columnsList.getSelectedValue()).clone();
            arrayObj.columns.add(newCol);
        }
        int index = columnsList.getSelectedIndex();
        update();
        columnsList.setSelectedIndex(index);
    }//GEN-LAST:event_AddButtonActionPerformed

    private void upButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upButtonActionPerformed
        
        if(columnsList.getSelectedIndex() <= 0){
            //do nothing, we are at the top
        }else{
            DataColumn tmp = arrayObj.columns.get(columnsList.getSelectedIndex());
            arrayObj.columns.set(columnsList.getSelectedIndex(), 
                    arrayObj.columns.get(columnsList.getSelectedIndex()-1));
            arrayObj.columns.set(columnsList.getSelectedIndex()-1, tmp);
            int index = columnsList.getSelectedIndex() - 1;
            update();
            columnsList.setSelectedIndex(index);
        }
        
        
    }//GEN-LAST:event_upButtonActionPerformed

    private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteButtonActionPerformed
        if(columnsList.getSelectedIndex() >= 0){
            arrayObj.columns.remove(columnsList.getSelectedValue());
            update();
        }
    }//GEN-LAST:event_DeleteButtonActionPerformed

    private void downButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downButtonActionPerformed
        //if the selected item isn't the last item in the list, go ahead
        // otherwise, do nothing
        if(!(columnsList.getSelectedIndex() >= arrayObj.columns.size()-1)){
            DataColumn tmp = arrayObj.columns.get(columnsList.getSelectedIndex()+1);
            arrayObj.columns.set(columnsList.getSelectedIndex()+1, 
                    arrayObj.columns.get(columnsList.getSelectedIndex()));
            arrayObj.columns.set(columnsList.getSelectedIndex(), tmp);
            int index = columnsList.getSelectedIndex();
            update();
            columnsList.setSelectedIndex(index+1);
        }
    }//GEN-LAST:event_downButtonActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
        ((DataColumn)columnsList.getSelectedValue()).displaySettingsDialog();
        update();
    }//GEN-LAST:event_editButtonActionPerformed

    private void columnsListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_columnsListMouseClicked
        if(evt.getClickCount()==2){
            ((DataColumn)columnsList.getSelectedValue()).displaySettingsDialog();
            update();
        }
    }//GEN-LAST:event_columnsListMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        arrayObj.dtParser.getSettingsDialog().setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void update(){
        columnsList.setListData(arrayObj.columns.toArray());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JButton DeleteButton;
    private javax.swing.JLabel arrayIDLabel;
    private javax.swing.JList columnsList;
    private javax.swing.JButton downButton;
    private javax.swing.JButton editButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton upButton;
    // End of variables declaration//GEN-END:variables
    
}
